package cn.sofwin.demo.web.dao;

import java.util.List;
import cn.sofwin.core.dao.BaseDao;
import cn.sofwin.core.utils.JdbcUtils;
import cn.sofwin.demo.web.entity.User;

/**
 * 用户数据库访问层
 * @author LetCode
 * @since 1.0
 */
public class UserDao extends BaseDao<User> {

    public User selectByUsername(String username){
        String sql = "select * from user where username = ?";
        return JdbcUtils.queryForObject(User.class, sql, username);
    }

    @Override
    public int insert(User user) {
        String sql = "insert into user(username, password, nickname) values (?, ?, ?)";
        return JdbcUtils.insert(sql, user.getUsername(), user.getPassword(), user.getNickname());
    }

    @Override
    public int deleteById(Integer id) {
        return JdbcUtils.update("delete from user where id = ?", id);
    }

    @Override
    public int update(User user) {
        String sql = "update user set username = ?, password = ?, nickname = ? where id = ?";
        return JdbcUtils.update(sql, user.getUsername(), user.getPassword(), user.getNickname(), user.getId());
    }

    @Override
    public User get(Integer id) {
        String sql = "select * from user where id = ?";
        return JdbcUtils.queryForObject(User.class, sql, id);
    }

    @Override
    public List<User> all() {
        String sql = "select * from user";
        return JdbcUtils.queryForList(User.class, sql);
    }
}
